package com.wpw.web;

import com.alibaba.fastjson.JSON;

import java.io.*;
import java.util.Set;

/**
 * @Description:
 * @ClassName: TransientTest
 * @CreateDate: 2019-12-21 00:10
 * @author: wupengwei
 */
public class TransientTest implements Serializable {

    private transient String name;

    private transient Long id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "TransientTest{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }

    private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
        System.out.println("read");
        s.defaultReadObject();
        name  = (String) s.readObject();
        id = (Long) s.readObject();

        // Write out size as capacity for behavioural compatibility with clone()
    }

    private void writeObject(ObjectOutputStream s) throws IOException {
        System.out.println("write");
        s.defaultWriteObject();
        s.writeObject(name);
        s.writeObject(id);
    }


    public static void main(String[] args) throws  Exception {
        TransientTest t = new TransientTest();
        t.setName("name");
        t.setId(1L);
        System.out.println("JSON:" + JSON.toJSONString(t));
        System.out.println(t);
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/Users/set.obj"))) {
            oos.writeObject(t);
        }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("/Users/set.obj"))) {
            t = (TransientTest)ois.readObject();
        }
        System.out.println(t);

        String s = JSON.toJSONString(t);
        System.out.println("JSON:" + s);

    }




}